package com.turing.data.engine.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.gson.JsonArray;
import com.turing.common.entity.TextVO;
import com.turing.common.util.FileUtils;
import com.turing.common.util.StringUtils;
import org.springframework.boot.test.json.JsonContentAssert;

public class ExplainFormData {

    public static void main(String[] args) {

        File path = new File("D:\\pdf\\form");
        File[] files = path.listFiles();
        List<TextVO> textVOS = new ArrayList<>();
        List<Form> forms = new ArrayList<>();

        assert files != null;
        for (File file : files) {
            if (!file.isDirectory()) { //判断是否文件夹
                String str = FileUtils.readFile(file.getAbsolutePath());
                List<Form> formList = JSONArray.parseArray(str, Form.class);
                forms.addAll(formList);
            }
        }

        for (Form form : forms) {

            TextVO textVO = new TextVO();
            textVO.setDocID(form.getDocID());
            textVO.setPageNo(form.getPageNo());
            List<TextVO.Paragraph> paragraphs = new ArrayList<>();
            List<Form.LineVO> lineList = form.getLineList();
            List<TextVO.Line> lineVOS = generateLineList(lineList, form.getPageNo());
            TextVO.Paragraph paragraph = new TextVO.Paragraph();
            paragraph.setParaId("0");
            paragraph.setParaNo("0");
            paragraph.setLines(lineVOS);
            paragraphs.add(paragraph);
            textVO.setParagraphs(paragraphs);
            textVOS.add(textVO);
        }

        //voidPage(textVOS);

        StringToFile(JSON.toJSONString(textVOS), "D:\\pdf\\form\\财务报表TEST3.json");

    }

    public static void voidPage(List<TextVO> textVOS) {
        TextVO textVO0 = new TextVO();
        textVO0.setPageNo(5);
        textVO0.setDocID("oxxxx");
        textVO0.setParagraphs(new ArrayList<>());

        List<TextVO.Paragraph> paragraphs0 = new ArrayList<>();
        TextVO.Paragraph paragraph0 = new TextVO.Paragraph();
        TextVO.Line line0 = new TextVO.Line();
        line0.setLineId("sdfsdfs");
        JSONArray jsonElements = new JSONArray(Arrays.asList(0,0,0,0));
        jsonElements.add(new ArrayList<Integer>());
        line0.setObjPos(jsonElements);
        line0.setObjContent("");
        line0.setLineNo(0);
        line0.setObjType("text");
        List<TextVO.Line> lines = new ArrayList<>();
        lines.add(line0);
        paragraph0.setLines(lines);
        paragraph0.setParaId("0");
        paragraph0.setParaNo("0");
        paragraphs0.add(paragraph0);
        textVO0.setParagraphs(paragraphs0);
        textVOS.add(textVO0);
    }

    private static List<TextVO.Line> generateLineList(List<Form.LineVO> lineList, Integer pageNo) {

        List<TextVO.Line> result = new ArrayList<>();

        int height = 0;
        int lineNo = -1;
        for (Form.LineVO lineVO : lineList) {
            Integer H = lineVO.getObjPos().get(0);
            if (Math.abs(H - height) > 8) {
                lineNo++;
                height = H;
            }
            lineVO.setLineNo(lineNo);
        }
        Map<Integer, List<Form.LineVO>> sortMap = new TreeMap<>(new MapKeyIntegerComparator());
        Map<Integer, List<Form.LineVO>> lines = lineList.stream().collect(Collectors.groupingBy(Form.LineVO::getLineNo));
        sortMap.putAll(lines);

        int it = 0;
        for (List<Form.LineVO> line : sortMap.values()) {
            StringBuilder sb = new StringBuilder();
            TextVO.Line line0 = new TextVO.Line();
            for (Form.LineVO test : line) {
                String text = test.getObjContent();
                if (!StringUtils.isEmpty(text)) {
                    sb.append(text).append("：");
                }
            }
            line0.setLineId("line_" + pageNo + "_" + it + "_" + StringUtils.genRandomNum());
            line0.setLineNo(it);
            if (sb.toString().length() >= 1) {
                line0.setObjContent(sb.toString().substring(0, sb.toString().length() - 1));
            } else {
                line0.setObjContent("");
            }
            JSONArray jsonElements = new JSONArray(Arrays.asList(0,0,0,0));
            jsonElements.add(new ArrayList<Integer>());
            line0.setObjPos(jsonElements);
            result.add(line0);
            it++;
        }

        return result;

    }

    private static class MapKeyIntegerComparator implements Comparator<Integer> {
        @Override
        public int compare(Integer o1, Integer o2) {
            return o1 - o2;
        }
    }

    public static void StringToFile(String content, String path) {

        try {
            File filename = new File(path);
            filename.createNewFile();
            FileWriter fw = new FileWriter(filename);

            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename), "utf-8")));
            out.write(content);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
